﻿using EFFC.Frame.Net.Module.Extend.EWRA.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFFC.Extends.LinqDLR2SQL;
using EFFC.Frame.Net.Unit.DB;
using EFFC.Frame.Net.Base.Data.Base;
using EFFC.Frame.Net.Module.Extend.EWRA.Attributes.Validation;
using EFFC.Frame.Net.Base.Common;
using EFFC.Frame.Net.Base.Data;
using EFFC.Frame.Net.Module.Business.Datas;
using ICPFrameLib.Business;
using ICPFrameLib.Attributes;
using EFFC.Frame.Net.Unit.DB.Parameters;

namespace XHRPMIS.Business.v1._0
{
    public class ICPBIDashboard: MyRestLogic
    {
        [EWRARoute("get", "/bi_dashboard/list")]
        [EWRARouteDesc("获取所有仪表板的设定列表")]
        [EWRAOutputDesc("返回结果", @"{
code:'success-成功，failed-失败',
msg:'提示信息',
data:[{
uid:'资料PK',
title:'名称',
desc:'描述',
route:'路由',
settings_json:'配置信息'
}]
}")]
        object GetAll()
        {
            SetCacheEnable(false);
            var filter = ComFunc.nvl(QueryStringD.filter);
            var metaup = DB.NewMetaUnitParameter();

            var re = (from t in DB.LamdaTable(metaup, "EXTEND_BI_DASHBOARD", "a")
                      orderby t.Title
                      select new
                      {
                          uid = t.UID,
                          title = t.Title,
                          desc = t.Desc,
                          route = t.Route,
                          settings_json = t.SettingsJson
                      }).GetQueryList(metaup);

            return new
            {
                code = "success",
                msg = "",
                data = re
            };
        }
        [EWRARoute("get", "/bi_dashboard/detail")]
        [EWRARouteDesc("根据UID或名称获取仪表板明细")]
        [EWRAAddInput("id", "string", "UID或名称", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.QueryString, true)]
        [EWRAOutputDesc("返回结果", @"{
code:'success-成功，failed-失败',
msg:'提示信息',
data:{
uid:'资料PK',
title:'名称',
desc:'描述',
route:'路由',
settings_json:'配置信息'
}
}")]
        object GetDetail()
        {
            var id = ComFunc.nvl(QueryStringD.id);
            SetCacheEnable(false);
            var metaup = DB.NewMetaUnitParameter();
            var info = (from t in DB.LamdaTable(metaup, "EXTEND_BI_DASHBOARD", "a")
                        where t.UID == id || t.Title == id
                        select new
                        {
                            uid = t.UID,
                            title = t.Title,
                            desc = t.Desc,
                            route = t.Route,
                            settings_json = t.SettingsJson
                        }).GetQueryList(metaup).FirstOrDefault();

            if(info == null)
            {
                return new
                {
                    code = "failed",
                    msg = "无该资料"
                };
            }
            return new
            {
                code = "success",
                msg = "",
                data = info
            };
        }
        [ActionAuth("BIDashboard")]
        [EWRARoute("get", "/icpbidashboard")]
        [EWRARouteDesc("获取仪表板的设定列表")]
        [EWRAAddInput("limit", "int", "每页笔数，当mode为Normal时需要提供", "默认为10", EFFC.Frame.Net.Base.Constants.RestInputPosition.QueryString, true)]
        [EWRAAddInput("page", "int", "指定页数，当mode为Normal时需要提供", "默认为1", EFFC.Frame.Net.Base.Constants.RestInputPosition.QueryString, true)]
        [EWRAAddInput("filter", "string", "过滤条件", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.QueryString, true)]
        [EWRAOutputDesc("返回结果", @"{
code:'success-成功，failed-失败',
msg:'提示信息',
data:{
TotalRow:'总笔数',
CurrentPage:'当前页数',
TotalPage:'总页数',
Count_Of_OnePage:'每页笔数',
data:[{
uid:'资料PK',
title:'名称',
desc:'描述',
route:'路由',
settings_json:'配置信息'
}
}")]
        object GetList()
        {
            SetCacheEnable(false);
            var filter = ComFunc.nvl(QueryStringD.filter);
            var metaup = DB.NewMetaUnitParameter();

            var re = (from t in DB.LamdaTable(metaup, "EXTEND_BI_DASHBOARD", "a")
                      where t.notnull(filter, t.Title.contains(filter))
                      select new
                      {
                          uid = t.UID,
                          title = t.Title,
                          desc = t.Desc,
                          route = t.Route,
                          settings_json = t.SettingsJson
                      }).QueryByPage(metaup, "Title");

            return ResultHelper.DFormQueryByPageFormat(re);
        }

        [ActionAuth("BIDashboard")]
        [EWRAEmptyValid("title")]
        [EWRARouteDesc("新增一个BI仪表板设定")]
        [EWRAAddInput("title", "string", "名称", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
        [EWRAAddInput("desc", "string", "描述", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, true)]
        [EWRAAddInput("route", "string", @"路由", "无默认值 ", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, true)]
        [EWRAAddInput("settings_json", "string", @"页面设定参数集，提供UI使用", "无默认值 ", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, true)]
        [EWRAOutputDesc("返回结果", @"{
code:""success-成功，failed-失败"",
msg:""提示信息"",
uid:""成功时候返回的UID""
}")]
        public override object post()
        {
            var title = ComFunc.nvl(PostDataD.title);
            var desc = ComFunc.nvl(PostDataD.desc);
            var route = ComFunc.nvl(PostDataD.route);
            var settings_json = ComFunc.nvl(PostDataD.settings_json);
            var uid = Guid.NewGuid().ToString();
            var metaup = DB.NewMetaUnitParameter();
            BeginTrans();
            if ((from t in DB.LamdaTable(metaup, "EXTEND_BI_DASHBOARD", "a")
                 where t.Title == title
                 select t).IsExists(metaup))
            {
                return new
                {
                    code = "failed",
                    msg = "名称重复"
                };
            }
           
            DB.QuickInsert(metaup, "EXTEND_BI_DASHBOARD", new
            {
                UID = uid,
                Title = title,
                Desc = desc,
                route = route,
                SettingsJson = settings_json,
                add_id = TokenPayLoad.ID,
                add_name = ComFunc.nvl(TokenPayLoad["name"]),
                add_ip = ClientInfo.IP,
                add_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            });

            CommitTrans();
            return new
            {
                code = "success",
                msg = "操作成功",
                uid
            };
        }
        [ActionAuth("BIDashboard")]
        [EWRAEmptyValid("title")]
        [EWRARouteDesc("修改一个BI仪表板设定")]
        [EWRAAddInput("id", "string", "资料PK", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.Path, false)]
        [EWRAAddInput("title", "string", "名称", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
        [EWRAAddInput("desc", "string", "描述", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, true)]
        [EWRAAddInput("route", "string", @"路由", "无默认值 ", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, true)]
        [EWRAAddInput("settings_json", "string", @"页面设定参数集，提供UI使用", "无默认值 ", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, true)]
        [EWRAOutputDesc("返回结果", @"{
code:""success-成功，failed-失败"",
msg:""提示信息""
}")]
        public override object patch(string id)
        {
            var title = ComFunc.nvl(PostDataD.title);
            var desc = ComFunc.nvl(PostDataD.desc);
            var route = ComFunc.nvl(PostDataD.route);
            var settings_json = ComFunc.nvl(PostDataD.settings_json);
            var metaup = DB.NewMetaUnitParameter();
            BeginTrans();
            dynamic info = (from t in DB.LamdaTable(metaup, "EXTEND_BI_DASHBOARD", "a")
                            where t.UID == id
                            select t).GetQueryList(metaup).FirstOrDefault();
            if(info == null)
            {
                return new
                {
                    code = "failed",
                    msg = "该资料不存在"
                };
            }
            if ((from t in DB.LamdaTable(metaup, "EXTEND_BI_DASHBOARD", "a")
                 where t.Title == title && t.UID != id
                 select t).IsExists(metaup))
            {
                return new
                {
                    code = "failed",
                    msg = "名称重复"
                };
            }

            DB.QuickUpdate(metaup, "EXTEND_BI_DASHBOARD", new
            {
                Title = title,
                Desc = desc,
                route = route,
                SettingsJson = settings_json,
                last_id = TokenPayLoad.ID,
                last_name = ComFunc.nvl(TokenPayLoad["name"]),
                last_ip = ClientInfo.IP,
                last_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            },new
            {
                info.UID
            });

            CommitTrans();
            return new
            {
                code = "success",
                msg = "操作成功"
            };
        }
        [ActionAuth("BIDashboard")]
        [EWRARouteDesc("删除一个BI仪表板设定")]
        [EWRAAddInput("id", "string", "资料PK", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.Path, false)]
        [EWRAOutputDesc("返回结果", @"根据http的状态码来识别，204标识操作成功，404表示操作识别未找到删除的资料")]
        public override bool delete(string id)
        {
            var metaup = DB.NewMetaUnitParameter();
            DB.QuickDelete(metaup, "EXTEND_BI_DASHBOARD", new
            {
                UID = id
            });
            return true;
        }
    }
}
